//
//  SHTextFieldAlertController.m
//  SHWheel
//
//  Created by YYKJ on 2021/2/22.
//  Copyright © 2021 YYKJ. All rights reserved.
//

#import "SHTextFieldAlertController.h"
#import "UIView+XXExtension.h"
#import "UIColor+XXExtension.h"


@interface SHTextFieldAlertController () <QMUIModalPresentationContentViewControllerProtocol, QMUIModalPresentationViewControllerDelegate, UITextFieldDelegate>

@property(nonatomic, strong, readwrite) QMUIModalPresentationViewController *modalPresentationViewController;
@property(nonatomic, assign) CGFloat keyboardHeight;

@property (nonatomic, strong) UIControl *maskView;
@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textfield1;
@property (nonatomic, strong) UITextField *textfield2;
@property (nonatomic, strong) UITextField *textfield3;

@end

@implementation SHTextFieldAlertController

#pragma mark - Life circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.maskView];
    [self.view addSubview:self.containerView];
    
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.textfield1];
    [self.containerView addSubview:self.textfield2];
    [self.containerView addSubview:self.textfield3];
}

#pragma mark - Layout
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat containerWidth = SCREEN_WIDTH - 40;
    CGFloat textfieldwidth = containerWidth - 40;
    self.maskView.frame = self.view.bounds;
    self.containerView.frame = CGRectMake(20, 200, SCREEN_WIDTH - 40, 300);
    
    self.titleLabel.frame = CGRectMake(0, 5, containerWidth, 30);
    self.textfield1.frame = CGRectMake(20, 40, textfieldwidth, 50);
    self.textfield2.frame = CGRectMake(20, self.textfield1.maxY + 50, textfieldwidth, 50);
    self.textfield3.frame = CGRectMake(20, self.textfield2.maxY + 50, textfieldwidth, 50);
}

#pragma mark - Helper
- (void)initModalPresentationController {
    self.modalPresentationViewController = [[QMUIModalPresentationViewController alloc] init];
    self.modalPresentationViewController.delegate = self;
    self.modalPresentationViewController.maximumContentViewWidth = CGFLOAT_MAX;
    self.modalPresentationViewController.contentViewMargins = UIEdgeInsetsZero;
    self.modalPresentationViewController.dimmingView = nil;
    self.modalPresentationViewController.contentViewController = self;
    [self customModalPresentationControllerAnimation];
}

- (void)customModalPresentationControllerAnimation {
    __weak __typeof(self)weakSelf = self;
    
    self.modalPresentationViewController.layoutBlock = ^(CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewDefaultFrame) {
        weakSelf.view.frame = CGRectMake(0, 0, CGRectGetWidth(containerBounds), CGRectGetHeight(containerBounds));
        weakSelf.keyboardHeight = keyboardHeight;
        [weakSelf.view setNeedsLayout];
    };
    
    self.modalPresentationViewController.showingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewFrame, void(^completion)(BOOL finished)) {
        weakSelf.maskView.alpha = 1;
        !completion ?: completion(YES);
    };
    
    self.modalPresentationViewController.hidingAnimation = ^(UIView *dimmingView, CGRect containerBounds, CGFloat keyboardHeight, void(^completion)(BOOL finished)) {
        weakSelf.maskView.alpha = 0;
        !completion ?: completion(YES);
    };
}

#pragma mark - UIControl event
- (void)handleMaskViewEvent:(id)sender {
    [self hideWithAnimated:YES completion:NULL];
}

#pragma mark - show & hide

- (void)showWithAnimated:(BOOL)animated {
    __weak typeof(self) weakSelf = self;
    [self initModalPresentationController];
    [self.modalPresentationViewController showWithAnimated:YES completion:^(BOOL finished) {
        weakSelf.maskView.alpha = 1;
    }];
}

- (void)hideWithAnimated:(BOOL)animated {
    [self hideWithAnimated:animated completion:NULL];
}

- (void)hideWithAnimated:(BOOL)animated completion:(void (^)(void))completion {
    __weak typeof(self) weakSelf = self;
    [self.modalPresentationViewController hideWithAnimated:YES completion:^(BOOL finished) {
        weakSelf.modalPresentationViewController = nil;
        weakSelf.maskView.alpha = 0;
        
        !completion ?: completion();
    }];
}

- (void)hideModalPresentationComponent {
    [self hideWithAnimated:YES];
}

#pragma mark - Getter & Setter
- (UIControl *)maskView {
    if (!_maskView) {
        _maskView = [[UIControl alloc] init];
        _maskView.alpha = 0;
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.35];
        [_maskView addTarget:self action:@selector(handleMaskViewEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskView;
}

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = UIColor.whiteColor;
    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"title";
    }
    return _titleLabel;
}

- (UITextField *)textfield1 {
    if (!_textfield1) {
        _textfield1 = [[UITextField alloc] init];
        _textfield1.backgroundColor = [UIColor xx_colorWithHexString:@"F5F5F5"];
    }
    return _textfield1;
}

- (UITextField *)textfield2 {
    if (!_textfield2) {
        _textfield2 = [[UITextField alloc] init];
        _textfield2.backgroundColor = [UIColor xx_colorWithHexString:@"F5F5F5"];
    }
    return _textfield2;
}

- (UITextField *)textfield3 {
    if (!_textfield3) {
        _textfield3 = [[UITextField alloc] init];
        _textfield3.backgroundColor = [UIColor xx_colorWithHexString:@"F5F5F5"];
    }
    return _textfield3;
}

@end
